package cn.wolfcode.p2p.base.controller;

import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.domain.VideoAuth;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.IOrderTimeService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthService;
import cn.wolfcode.p2p.util.*;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 前台视频认证
 */
@Controller
public class VideoOrderController {

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IVideoAuthService videoAuthService;
    @Autowired
    private ILoginInfoService loginInfoService;
    @Autowired
    private IOrderTimeService orderTimeService;


    //视频预约
    @RequestMapping("videoAuditOrder")
    public String videoAuditOrder(Model model) {

        //获取登录过的用户id
        UserInfo userInfo = userInfoService.getById(UserContext.getLoginInfo().getId());

        //存在位状态代表通过视频认证---
        if (userInfo.hasVedioAuth()) {
            model.addAttribute("videoSuccess", true);

        } else if (userInfo.getVideoAuthId() != null) {  //预约成功 -->  预约id不为null
            //前台要显示客服,日期,时间
            VideoAuth videoAuth = videoAuthService.getById(userInfo.getVideoAuthId());
            model.addAttribute("bookSuccess", videoAuth);
            model.addAttribute("videoAuditOrder", true);

        } else {
            //查询所有的审核客服
            List auditors = loginInfoService.listAuditors();
            model.addAttribute("auditors", auditors);
            //查询所有的预约日期
            List<Date> list = new ArrayList<>();
            Date date = new Date();
            list.add(DateUtils.addDays(date, 1));
            list.add(DateUtils.addDays(date, 2));
            list.add(DateUtils.addDays(date, 3));
            model.addAttribute("orderDates", list);
            //查询所有的预约时间
            model.addAttribute("orderTimes", orderTimeService.selectAll());
        }
        return "videoOrder";
    }

    /**
     * 接收前台传来的客服,日期,时间
     *
     * @return
     */
    @RequestMapping("saveVideoAuditOrder")
    @ResponseBody
    public JsonResult saveVideoAuditOrder(VideoAuth videoAuth) {
        videoAuthService.apply(videoAuth);
        return JsonResult.instance();
    }


}
